/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.util;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.KeySlice;

/**
 * 
 * @author bloodyparadise
 */
public class StorageManager {

	private String keySpace;
	private String conlumnFamily;
	private String rowKey;

	/**
	 * 
	 * @param keySpace parameter
	 * @param conlumnFamily parameter
	 * @return
	 */
	public StorageManager(String keySpace, String conlumnFamily) {
		this.keySpace = keySpace;
		this.conlumnFamily = conlumnFamily;
		CassandraUtil.getInstance(keySpace);
	}

	/**
	 * 
	 * @return
	 */
	public String getConlumnFamily() {
		return conlumnFamily;
	}

	/**
	 * 
	 * @param conlumnFamily parameter
	 * @return
	 */
	public void setConlumnFamily(String conlumnFamily) {
		this.conlumnFamily = conlumnFamily;
	}

	/**
	 * 
	 * @return
	 */
	public String getKeySpace() {
		return keySpace;
	}

	/**
	 * 
	 * @param keySpace parameter
	 * @return
	 */
	public void setKeySpace(String keySpace) {
		this.keySpace = keySpace;
	}

	/**
	 * 
	 * @return
	 */
	public String getRowKey() {
		return rowKey;
	}

	/**
	 * 
	 * @param column1 parameter
	 * @param name parameter
	 * @param column2 parameter
	 * @return
	 */
	public List<KeySlice> getAllInOneColumnByAnother(String column1,
			String name, String column2) {
		List<KeySlice> crs = null;
		try {
			crs = CassandraUtil.selectAllInOneColumnByAnother(keySpace,
					conlumnFamily, column1, name, column2);
		} catch (Exception ex) {
			Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return crs;
	}

	/**
	 * 
	 * @param rowKey parameter
	 * @return
	 */
	public void setRowKey(String rowKey) {
		this.rowKey = rowKey;
	}

	/**
	 * 
	 * @param column parameter
	 * @param value parameter
	 * @return
	 */
	public void put(String column, String value) {
		try {
			CassandraUtil
					.update(keySpace, conlumnFamily, rowKey, column, value);
		} catch (Exception ex) {
			Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	// public void delete(String column, String value) {
	// try {
	// CassandraUtil.delete(keySpace, conlumnFamily, rowKey, column, value);
	// } catch (Exception ex) {
	// Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null,
	// ex);
	// }
	// }

	/**
	 * 
	 * @param column parameter
	 * @param value parameter
	 * @return
	 */
	public List<KeySlice> get(String column, String value) {
		List<KeySlice> crs = null;
		try {
			crs = CassandraUtil.select(keySpace, conlumnFamily, column, value);
		} catch (Exception ex) {
			Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return crs;
	}

	/**
	 * 
	 * @param column parameter
	 * @param value parameter
	 * @param column1 parameter
	 * @param value1 parameter
	 * @return
	 */
	public List<KeySlice> getByTwoColumn(String column, String value,
			String column1, String value1) {
		List<KeySlice> crs = null;
		try {
			crs = CassandraUtil.selectByTwoColumn(keySpace, conlumnFamily,
					column, value, column1, value1);
		} catch (Exception ex) {
			Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return crs;
	}

	/**
	 * 
	 * @return
	 */
	public List<KeySlice> getAll() {
		List<KeySlice> crs = null;
		try {
			crs = CassandraUtil.selectAll(keySpace, conlumnFamily);
		} catch (Exception ex) {
			Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return crs;
	}

	/**
	 * 
	 * @param column parameter
	 * @return
	 */
	public List<KeySlice> getAllInOneColumn(String column) {
		List<KeySlice> crs = null;
		try {
			crs = CassandraUtil.selectAllInOneColumn(keySpace, conlumnFamily,
					column);
		} catch (Exception ex) {
			Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		return crs;
	}

	/**
	 * 
	 * @param rowkey parameter
	 * @return
	 */
	public void delete(String rowkey) {
		try {
			CassandraUtil.delete(keySpace, conlumnFamily, "key", rowkey);
		} catch (Exception ex) {
			Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	/**
	 * 
	 * @return
	 */
	public void open() {
		CassandraUtil.open(keySpace);
	}

	/**
	 * 
	 * @return
	 */
	public void close() {
		CassandraUtil.close(keySpace);
	}
}
